<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class CI_Country_city{
	var $CI;
	public function __construct( ){
		$this->CI =& get_instance();
	}
	
    function getListRecords_country( ){
		$rs = $this->CI->db->get( TBL_COUNTRY )->result();
		return $rs;
	}
	
	function getCountryRecordBy_id($id){
		return $this->CI->db->where('id',$id)->get(TBL_COUNTRY)->result();
	}
	
	function getCountryRecordsBy_zonecode($zoneCode){
		return $this->CI->db->order_by('name','asc')->where('country_origin_code', $zoneCode)->get(TBL_COUNTRY)->result();
	}
	
	function getCountryRecordBy_name($name){
		return $this->CI->db->where('name',$name)->get(TBL_COUNTRY)->result();
	}
	
	function deleteCountryRecord_id($id){
		$this->CI->db->where('id',$id)->delete(TBL_COUNTRY);
		return true;
	}
	
	function getListRecords_city(){
		return $this->CI->db->select("CITY.*,COUNTRY.name AS country")
				->from(TBL_CITY." AS CITY")
				->join(TBL_COUNTRY." AS COUNTRY", "COUNTRY.id = CITY.country_id","left")
				->get()
				->result();
	}
	
	function getCityRecordBy_name($name){
		return $this->CI->db->where('name',$name)->get(TBL_CITY)->result();
	}
	
	function getCityRecordBy_code($code){
		return $this->CI->db->where('code',$code)->get(TBL_CITY)->result();
	}
	
	function getCityRecordBy_id($id){
		return $this->CI->db->where('id',$id)->get(TBL_CITY)->result();
	}
	
	function getCityRecordsBy_countryid($countryId){
		return $this->CI->db->order_by('name','asc')->where('country_id',$countryId)->get(TBL_CITY)->result();
	}
	
	function deleteCityRecord_id($id){
		$this->CI->db->where('id',$id)->delete(TBL_CITY);
		return true;
	}
	
	//admin
	function saveCountry($id, $name, $code, $country_origin_code){
		if($id == 0){
			//$recordRS = $this->getCountryRecordBy_name($name);
			//if(!empty($recordRS)){
			//	return false;
			//}
			//create new
			$this->CI->db->insert( TBL_COUNTRY, array('name'=> $name, 'code'=>$code, 'country_origin_code'=> $country_origin_code) );
			return true;
		}else{
			//$recordRS = $this->getCountryRecordBy_name($name);
			//if(count($recordRS)<= 1 AND $recordRS[0]->id == $id){
				$this->CI->db->where('id',$id)->update( TBL_COUNTRY, array('name'=> $name, 'code'=>$code, 'country_origin_code'=> $country_origin_code) );
				return true;
			//}else{
			//	return false;
			//}
		}
	}
	
	//admin
	function saveCity($id, $name, $code, $country_id){
		if($id == 0){
			$recordRS = $this->getCityRecordBy_code($code);
			if(!empty($recordRS)){
				return false;
			}
			//create new
			$this->CI->db->insert( TBL_CITY, array('name'=> $name, 'code'=>$code, 'country_id'=>$country_id) );
			return true;
		}else{
			$recordRS = $this->getCityRecordBy_code($code);
			
			if( count($recordRS) <= 1 AND $recordRS[0]->id == $id ){
				$this->CI->db->where('id',$id)->update( TBL_CITY, array('name'=> $name, 'code'=>$code, 'country_id'=>$country_id) );
				return true;
			}else{
				return false;
			}
		}
	}
	
}

?>